package com.sqlparser.core.parser.impl;

import com.sqlparser.core.lexer.Lexer;
import com.sqlparser.core.lexer.Token;
import com.sqlparser.core.lexer.TokenType;
import com.sqlparser.core.parser.Parser;
import com.sqlparser.core.parser.ast.*;
import com.sqlparser.common.exception.SQLParserException;

import java.util.ArrayList;
import java.util.List;

/**
 * 简单的SQL语法分析器实现
 */
public class SimpleParser implements Parser {
    private final Lexer lexer;
    private Token currentToken;
    
    public SimpleParser(Lexer lexer) {
        this.lexer = lexer;
        this.currentToken = lexer.nextToken();
    }
    
    @Override
    public SQLStatement parse() {
        switch (currentToken.getType()) {
            case SELECT:
                return parseSelectStatement();
            case DELETE:
                return parseDeleteStatement();
            default:
                throw new SQLParserException("Expected SELECT or DELETE keyword");
        }
    }
    
    private SelectStatement parseSelectStatement() {
        SelectStatement stmt = new SelectStatement();
        
        // 跳过SELECT关键字
        currentToken = lexer.nextToken();
        
        // 解析列名列表
        parseColumns(stmt);
        
        // 解析FROM子句
        if (currentToken.getType() != TokenType.FROM) {
            throw new SQLParserException("Expected FROM keyword");
        }
        currentToken = lexer.nextToken();
        
        // 解析表名
        parseTableName(stmt);
        
        // 解析WHERE子句（如果有）
        if (currentToken != null && currentToken.getType() == TokenType.WHERE) {
            stmt.setWhereClause(parseWhereClause());
        }
        
        return stmt;
    }
    
    private DeleteStatement parseDeleteStatement() {
        DeleteStatement stmt = new DeleteStatement();
        
        // 跳过DELETE关键字
        currentToken = lexer.nextToken();
        
        // 解析FROM子句
        if (currentToken.getType() != TokenType.FROM) {
            throw new SQLParserException("Expected FROM keyword after DELETE");
        }
        currentToken = lexer.nextToken();
        
        // 解析表名
        parseTableName(stmt);
        
        // 解析WHERE子句（如果有）
        if (currentToken != null && currentToken.getType() == TokenType.WHERE) {
            stmt.setWhereClause(parseWhereClause());
        }
        
        return stmt;
    }
    
    private void parseColumns(SelectStatement stmt) {
        while (true) {
            if (currentToken.getType() != TokenType.IDENTIFIER) {
                throw new SQLParserException("Expected column name");
            }
            
            stmt.getColumns().add(currentToken.getValue());
            currentToken = lexer.nextToken();
            
            if (currentToken.getType() != TokenType.COMMA) {
                break;
            }
            currentToken = lexer.nextToken();
        }
    }
    
    private void parseTableName(SQLStatement stmt) {
        if (currentToken.getType() != TokenType.IDENTIFIER) {
            throw new SQLParserException("Expected table name");
        }
        stmt.setTableName(currentToken.getValue());
        currentToken = lexer.nextToken();
    }
    
    private WhereClause parseWhereClause() {
        // 跳过WHERE关键字
        currentToken = lexer.nextToken();
        
        // 获取列名
        if (currentToken.getType() != TokenType.IDENTIFIER) {
            throw new SQLParserException("Expected column name in WHERE clause");
        }
        String column = currentToken.getValue();
        currentToken = lexer.nextToken();
        
        // 获取操作符
        if (currentToken.getType() != TokenType.OPERATOR) {
            throw new SQLParserException("Expected operator in WHERE clause");
        }
        String operator = currentToken.getValue();
        currentToken = lexer.nextToken();
        
        // 获取值
        if (currentToken.getType() != TokenType.NUMBER && 
            currentToken.getType() != TokenType.STRING) {
            throw new SQLParserException("Expected value in WHERE clause");
        }
        String value = currentToken.getValue();
        currentToken = lexer.nextToken();
        
        return new WhereClause(column, operator, value);
    }
} 